#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct node
{
	int data;
	struct node *next;
}*head;

void append(int num)
{
	struct node *temp,*right;
	temp= (struct node *)malloc(sizeof(struct node));
	temp->data=num;
	right=(struct node *)head;
	while(right->next != NULL)
	right=right->next;
	right->next =temp;
	right=temp;
	right->next=NULL;
}

void add( int num )
{
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    if (head== NULL)
    {
    head=temp;
    head->next=NULL;
    }
    else
    {
    temp->next=head;
    head=temp;
    }
}
void addafter(int num, int loc)
{
    int i;
    struct node *temp,*left,*right;
    right=head;
    for(i=1;i<loc;i++)
    {
    left=right;
    right=right->next;
    }
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    left->next=temp;
    left=temp;
    left->next=right;
    return;
}
 
 
 
void insert(int num)
{
    int c=0;
    struct node *temp;
    temp=head;
    if(temp==NULL)
    {
    add(num);
    }
    else
    {
    while(temp!=NULL)
    {
        if(temp->data<num)
        c++;
        temp=temp->next;
    }
    if(c==0)
        add(num);
    else if(c<count())
        addafter(num,++c);
    else
        append(num);
    }
}

void  print(struct node *r)
{
	r=head;
	if(r==NULL)
	{
	return;
	}
	while(r!=NULL)
	{
	printf("%d ",r->data);
	r=r->next;
	}
	printf("\n");
}

int count()
{
	struct node *n;
	int c=0;
	n=head;
	while(n!=NULL)
	{
	n=n->next;
	c++;
	}
	return c;
}

/* to call do these below:
struct node *adc;
head = NULL;
push_end(1,1,5);
count();
print(adc);
*/
void main()
{
	struct node *adc;
	head = NULL;

	append(1);
	append(5);
	append(8);
	append(0);
	count();
	print(adc);
}

